package com.morven.workpiece.craft.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.morven.workpiece.common.utils.DateUtils;
import com.morven.workpiece.craft.mapper.CraftProceWageCoefficMapper;
import com.morven.workpiece.craft.domain.CraftProceWageCoeffic;
import com.morven.workpiece.craft.service.ICraftProceWageCoefficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 工序工价系数Service业务层处理
 *
 * @author Morven
 * @date 2021-12-20
 */
@Service
public class CraftProceWageCoefficServiceImpl implements ICraftProceWageCoefficService {
    @Autowired
    private CraftProceWageCoefficMapper craftProceWageCoefficMapper;

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 查询工序工价系数
     *
     * @param id 工序工价系数主键
     * @return 工序工价系数
     */
    @Override
    public CraftProceWageCoeffic selectCraftProceWageCoefficById(Long id) {
        return craftProceWageCoefficMapper.selectCraftProceWageCoefficById(id);
    }

    /**
     * 查询工序工价系数列表
     *
     * @param craftProceWageCoeffic 工序工价系数
     * @return 工序工价系数
     */
    @Override
    public List<CraftProceWageCoeffic> selectCraftProceWageCoefficList(CraftProceWageCoeffic craftProceWageCoeffic) {
        return craftProceWageCoefficMapper.selectCraftProceWageCoefficList(craftProceWageCoeffic);
    }

    /**
     * 新增工序工价系数
     *
     * @param craftProceWageCoeffic 工序工价系数
     * @return 结果
     */
    @Override
    public int insertCraftProceWageCoeffic(CraftProceWageCoeffic craftProceWageCoeffic) {
        craftProceWageCoeffic.setCreateTime(DateUtils.getNowDate());
        return craftProceWageCoefficMapper.insertCraftProceWageCoeffic(craftProceWageCoeffic);
    }

    /**
     * 修改工序工价系数
     *
     * @param craftProceWageCoeffic 工序工价系数
     * @return 结果
     */
    @Override
    public int updateCraftProceWageCoeffic(CraftProceWageCoeffic craftProceWageCoeffic) {
        return craftProceWageCoefficMapper.updateCraftProceWageCoeffic(craftProceWageCoeffic);
    }

    /**
     * 批量删除工序工价系数
     *
     * @param ids 需要删除的工序工价系数主键
     * @return 结果
     */
    @Override
    public int deleteCraftProceWageCoefficByIds(Long[] ids) {
        return craftProceWageCoefficMapper.deleteCraftProceWageCoefficByIds(ids);
    }

    /**
     * 删除工序工价系数信息
     *
     * @param id 工序工价系数主键
     * @return 结果
     */
    @Override
    public int deleteCraftProceWageCoefficById(Long id) {
        return craftProceWageCoefficMapper.deleteCraftProceWageCoefficById(id);
    }

    /**
     * 初始加载工序系数需要的数据
     * 如：工序类别 categoryList
     *    标准工序单价 standardProcessPirce
     *    工序排序字段信息  processOrderBy
     */
    @Override
    public Map<String, Object> initCoefficData() {
        Map<String, Object> map = new HashMap<>();
        map.put("categoryList",selectCraftProceCategoryTypeList());
        map.put("standardProcessPrice",selectStandardProcessPrice());
        map.put("processOrderBy",selectCoefficeProcessOrderBy());
        return map;
    }

    /**
     * @author Morven
     * @date 2021/12/27 13:51
     * @description: 查询当前工序系数默认排序
     */
    private Object selectCoefficeProcessOrderBy() {
        return jdbcTemplate.queryForMap("select count(*)+1 as orderBy from craft_proce_wage_coeffic ").get("orderBy");
    }

    /**
     * @author Morven
     * @date 2021/12/27 13:51
     * @description: 查询标准工序单价
     */
    private Object selectStandardProcessPrice() {
        return jdbcTemplate.queryForMap("select standard_price from craft_standard_process_price where state =0").get("standard_price");
    }

    /**
     * @author Morven
     * @date 2021/12/27 9:35
     * @description: 查询工序类别
     */

    private List<Map<String, Object>> selectCraftProceCategoryTypeList() {
        List<Map<String, Object>> groupList = new ArrayList<>();
        List<Map<String, Object>> categoryList = jdbcTemplate.queryForList("select id,proce_category_type,parent_id from craft_proce_category_type where id not in (select proce_category_type_id from craft_proce_wage_coeffic) ");
        for (Map<String, Object> category : categoryList) {
            Long id = Long.parseLong(category.get("id").toString());
            Long parentId = Long.parseLong(category.get("parent_id").toString());
            String categoryName = category.get("proce_category_type").toString();
            if (parentId == 0) {
                Map<String, Object> tmp = new HashMap<>();
                tmp.put("id", id);
                tmp.put("label", categoryName);
                tmp.put("options", new ArrayList<>());
                groupList.add(tmp);
            }
        }

        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> group : groupList) {
            Long groupId = (Long) group.get("id");
            List<Map<String, Object>> options = new ArrayList<>();
            for (Map<String, Object> category : categoryList) {
                Long id = Long.parseLong(category.get("id").toString());
                Long parentId = Long.parseLong(category.get("parent_id").toString());
                String categoryName = category.get("proce_category_type").toString();
                if (groupId == parentId) {
                    Map<String, Object> tmp = new HashMap<>();
                    tmp.put("value", id);
                    tmp.put("label", categoryName);
                    options.add(tmp);
                }
            }
            group.put("options",options);
            if(options.size()>0){
                list.add(group);
            }
        }
        return list;
    }
}
